﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ZDT.Common
{
    /// <summary>
    /// xml系列化
    /// </summary>
    public class X2S
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="fileName"></param>
        public static void ObjToFile<T>(T obj, string fileName = "")
        {
            string result = string.Empty;
            Utility.CreatedPath(fileName);
            TextWriter writer = new StreamWriter(fileName);
            //序列化这个对象
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            //将对象序列化输出到控制台
            serializer.Serialize(writer, obj);
            writer.Close();
            writer.Dispose();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjToXml<T>(T obj)
        {
            string result = string.Empty;
            TextWriter writer =writer = new StringWriter();
            //序列化这个对象
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            //将对象序列化输出到控制台
            serializer.Serialize(writer, obj);
            result = writer.ToString();
            writer.Close();
            writer.Dispose();
            return result;
        }
        public static T FileToObj<T>(string filePath)
        {
            TextReader reader = new StreamReader(filePath);
            //序列化这个对象
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            //将对象序列化输出到控制台
            var result = (T)serializer.Deserialize(reader);
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T XmlToObj<T>(string xml)
        {
            TextReader reader = new StringReader(xml);
            //序列化这个对象
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            //将对象序列化输出到控制台
            var result = (T)serializer.Deserialize(reader);
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="fileName"></param>
        public static void ObjToDFile<T>(T obj, string fileName = "")
        {
            string result = string.Empty;
            Utility.CreatedPath(fileName);
            TextWriter writer = new StreamWriter(fileName);
            //序列化这个对象
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            //将对象序列化输出到控制台
            serializer.Serialize(writer, obj);
            writer.Close();
            writer.Dispose();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjToDXml<T>(T obj)
        {
            string result = string.Empty;
            TextWriter writer = writer = new StringWriter();
            //序列化这个对象
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            //将对象序列化输出到控制台
            serializer.Serialize(writer, obj);
            result = writer.ToString();
            writer.Close();
            writer.Dispose();
            return result;
        }
        public static T DFileToObj<T>(string filePath)
        {
            TextReader reader = new StreamReader(filePath);
            //序列化这个对象
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            //将对象序列化输出到控制台
            var result = (T)serializer.Deserialize(reader);
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T DXmlToObj<T>(string xml)
        {
            TextReader reader = new StringReader(xml);
            //序列化这个对象
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            //将对象序列化输出到控制台
            var result = (T)serializer.Deserialize(reader);
            return result;
        }
    }
}
